﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
//using System.Collections;
using System.Xml;
using System.Web.Services;

public partial class Services_MaintDispatch : System.Web.UI.Page
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HidWareid.Value = getWareId();
            initData();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            hfxs.Value = Request.QueryString["moduid"].ToString();
            yjTm1.Text = Request.QueryString["yjTm1"];
            yjTm2.Text = Request.QueryString["yjTm2"];
            Guesttel.Text = Request.QueryString["Guesttel"];
            MaintCode.Text = Request.QueryString["MaintCode"];
            Guestname.Text = Request.QueryString["Guestname"];
            comidSpe.Text = Request.QueryString["comidSpe"];
            code.Text = Request.QueryString["code"];
            fwcs.Text = Request.QueryString["fwcs"];
            Guestaddress.Text = Request.QueryString["Guestaddress"];
            maintmen.SelectedValue = Request.QueryString["maintmen"];
            initUser();
        }
    }

    private void initData()
    {
        string search = "";
        string sql = "select * from(select a.*,(select StaffName from CS_Staff where StaffId=a.maintmen) as StaffName from CS_Mailadd a) TblMailadd  where wcdate <>'' ";
        if (!string.IsNullOrEmpty(Request.QueryString["yjTm1"]) && !string.IsNullOrEmpty(Request.QueryString["yjTm2"]))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
            sql += " and CONVERT(char(10), wcdate, 120)>='" + Request.QueryString["yjTm1"] + "' and CONVERT(char(10), wcdate, 120)<='" + Request.QueryString["yjTm2"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Guesttel"]))
        {
            search += "&Guesttel=" + Common.FormatParameter(Request.QueryString["Guesttel"].Trim());
            sql += " and Guesttel like '%" + Request.QueryString["Guesttel"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["MaintCode"]))
        {
            search += "&MaintCode=" + Common.FormatParameter(Request.QueryString["MaintCode"].Trim());
            sql += " and MaintCode like '%" + Request.QueryString["MaintCode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Guestname"]))
        {
            search += "&Guestname=" + Common.FormatParameter(Request.QueryString["Guestname"].Trim());
            sql += " and Guestname like '%" + Request.QueryString["Guestname"] + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["comidSpe"]))
        {
            search += "&comidSpe=" + Common.FormatParameter(Request.QueryString["comidSpe"].Trim());
            sql += " and comidSpe like '%" + Request.QueryString["comidSpe"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["code"]))
        {
            search += "&code=" + Common.FormatParameter(Request.QueryString["code"].Trim());
            sql += " and (xlh2 like '%" + Request.QueryString["code"] + "%' or xlh1 like '%" + Request.QueryString["code"] + "%' or fwcs like '%" + Request.QueryString["code"] + "%')";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["fwcs"]))
        {
            search += "&fwcs=" + Common.FormatParameter(Request.QueryString["fwcs"].Trim());
            sql += " and fwcs like '%" + Request.QueryString["fwcs"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["Guestaddress"]))
        {
            search += "&Guestaddress=" + Common.FormatParameter(Request.QueryString["Guestaddress"].Trim());
            sql += " and Guestaddress like '%" + Request.QueryString["Guestaddress"] + "%'";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["maintmen"]))
        {
            search += "&maintmen=" + Common.FormatParameter(Request.QueryString["maintmen"].Trim());
            sql += " and maintmen =" + Request.QueryString["maintmen"] + "";
        }

        sql += " order by Maintaddid desc";
        //Response.Write(sql);
        //Response.End();
        PageNavigator1.Url = "../Services/MaintDispatch.aspx?moduid=" + Request.QueryString["moduid"].ToString() + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Request.Cookies["user"].Values["id"];
        zs.Open(sql);

        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }

    //生成用户
    private void initUser()
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=40 order by StaffName");
        maintmen.DataSource = name.m_table;
        maintmen.DataTextField = "StaffName";
        maintmen.DataValueField = "StaffId";
        maintmen.DataBind();
        maintmen.Items.Insert(0, new ListItem("所有", ""));
        name.Close();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = "";
        //string sql = "select * from(select *,Convert(char(10),dispTime,120) as Date,Convert(char(10),MaintDate,120) as Date1,Convert(char(10),yydate,120) as Date2 from CS_Mailadd where ispg=1 and isWC=0) TblMailadd ";

        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
        }
        if (!string.IsNullOrEmpty(Guesttel.Text.Trim()))
        {
            search += "&Guesttel=" + Common.FormatParameter(Guesttel.Text.Trim());
        }
        if (!string.IsNullOrEmpty(MaintCode.Text.Trim()))
        {
            search += "&MaintCode=" + MaintCode.Text.Trim();
        }
        if (!string.IsNullOrEmpty(Guestname.Text.Trim()))
        {
            search += "&Guestname=" + Common.FormatParameter(Guestname.Text.Trim());
        }
        if (!string.IsNullOrEmpty(comidSpe.Text.Trim()))
        {
            search += "&comidSpe=" + Common.FormatParameter(comidSpe.Text.Trim());
        }
        if (!string.IsNullOrEmpty(code.Text.Trim()))
        {
            search += "&code=" + Common.FormatParameter(code.Text.Trim());
        }
        if (!string.IsNullOrEmpty(fwcs.Text.Trim()))
        {
            search += "&fwcs=" + Common.FormatParameter(fwcs.Text.Trim());
        }
        if (!string.IsNullOrEmpty(Guestaddress.Text.Trim()))
        {
            search += "&Guestaddress=" + Common.FormatParameter(Guestaddress.Text.Trim());
        }
        if (!string.IsNullOrEmpty(maintmen.SelectedValue))
        {
            search += "&maintmen=" + Common.FormatParameter(maintmen.SelectedValue);
        }
        PageNavigator1.Url = "../Services/MaintDispatch.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("MaintDispatch.aspx?moduid=" + hfxs.Value + search);

    }
    protected void linkedit_Click(object sender, EventArgs e)//修改
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("DispatchEnd.aspx?moduid=" + Request.QueryString["moduid"] + "&id=" + id);
    }

    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 11, GvData);
    }

    protected void btnAdd_Click(object sender, EventArgs e)//添加客户报修单
    {
        Response.Redirect("Dispatch.aspx?moduid=" + Request["moduid"]);
    }

    protected void btnExcel_Click(object sender, EventArgs e)//生成EXCEL
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=ServiceGuest" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete CS_Mailadd  where MaintCode='" + this.GvData.Rows[i].Cells[0].Text + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }
}
